import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

import { wrapperEnv, createProxy } from './build/utils'
import { createVitePlugins } from './build/plugin'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const isBuild = command === 'build'

  const env = loadEnv(mode, process.cwd())

  const viteEnv = wrapperEnv(env)

  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = viteEnv

  return {
    base: VITE_PUBLIC_PATH || '/',
    // 代理的意义：在本地开发时，我们请求后端接口存在跨域，需要node server进行代理解决
    server: {
      host: '0.0.0.0',
      port: VITE_PORT,
      proxy: createProxy(VITE_PROXY),
    },
    resolve: {
      // 设置别名
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import '@/styles/variables.scss';`,
        },
      },
    },
    plugins: createVitePlugins(viteEnv, isBuild),
  }
})
